#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int T;
  cin >> T;
  while (T --) {
    int n;
    cin >> n;
    vector<int> a(n), b(n), d(n);
    for (int i = 0; i < n; i ++) cin >> a[i] >> b[i] >> d[i];
    vector<int> p = a;
    p.push_back(-1);
    p.insert(p.end(), b.begin(), b.end());
    sort(p.begin(), p.end());
    p.erase(unique(p.begin(), p.end()), p.end());
    for (int i = 0; i < n; i ++) {
      a[i] = lower_bound(p.begin(), p.end(), a[i]) - p.begin();
      b[i] = lower_bound(p.begin(), p.end(), b[i]) - p.begin();
    }
    int P = p.size();
    vector<vector<int>> dp(P + 2, vector<int>(P + 2));
    for (int l = P; l >= 1; l --) {
      for (int r = l; r <= P; r ++) {
        dp[l][r] = INT_MAX;
        int max_d = 0, max_id = 0;
        for (int i = 0; i < n; i ++) {
          if (a[i] < l || b[i] > r) continue;
          if (d[i] > max_d) {
            max_d = d[i];
            max_id = i;
          }
        }
        for (int k = a[max_id]; k <= b[max_id]; k ++) {
          dp[l][r] = min(dp[l][r], dp[l][k - 1] + dp[k + 1][r] + max_d);
        }
      }
    }
    cout << dp[1][P] << endl;
  }
}